IF EXISTS (SELECT * FROM sys.syslogins WHERE name='$(STAGING_DATABASE_USERNAME)')
BEGIN
    DROP LOGIN $(STAGING_DATABASE_USERNAME)
END
GO

CREATE LOGIN $(STAGING_DATABASE_USERNAME) WITH PASSWORD = '$(STAGING_DATABASE_PASSWORD)', DEFAULT_DATABASE=[$(STAGING_DATABASE_NAME)], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO

USE $(STAGING_DATABASE_NAME)
GO

IF EXISTS (SELECT * FROM sys.sysusers WHERE name='$(STAGING_DATABASE_USERNAME)')
BEGIN
    DROP USER $(STAGING_DATABASE_USERNAME)
END

CREATE USER $(STAGING_DATABASE_USERNAME) FOR LOGIN $(STAGING_DATABASE_USERNAME) 
GO   

GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[TEST]                 TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[USER]                 TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[EDUCATION_MODULE]     TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[MODULE_DEPENDENCE]    TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[ROLE]                 TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[EDUCATION_GROUP]      TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[USER2EDUCATION]       TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[GROUP2MODULE]         TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[GROUP2USER]           TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[FILTER]               TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[FILTER_FIELD]         TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[MODULE_DOC]           TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[DOC2MODULE]           TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[EVENT]                TO  $(STAGING_DATABASE_USERNAME)
GRANT ALTER, DELETE, INSERT, REFERENCES, SELECT, UPDATE ON [dbo].[EVENT2USER]           TO  $(STAGING_DATABASE_USERNAME)

GRANT EXECUTE ON [dbo].[createUserFilter] 	TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[createDocFilter] 	TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[createModuleFilter] 	TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[createGroupFilter] 	TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[createEducationFilter] 	TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[deleteUser] 		TO	$(STAGING_DATABASE_USERNAME)
GRANT EXECUTE ON [dbo].[deleteEducation]	TO	$(STAGING_DATABASE_USERNAME)


GRANT CREATE TABLE TO $(STAGING_DATABASE_USERNAME)
GRANT CREATE PROCEDURE TO $(STAGING_DATABASE_USERNAME)
GRANT ALTER ON SCHEMA::dbo TO $(STAGING_DATABASE_USERNAME)


GO
